package com.qf.electronic.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.electronic.common.R;
import com.qf.electronic.common.ResponseStatus;
import com.qf.electronic.dto.InspectionReceptionDto;
import com.qf.electronic.dto.InspectionReceptionStateDto;
import com.qf.electronic.pojo.InspectionReception;
import com.qf.electronic.pojo.User;
import com.qf.electronic.service.IInspectionReceptionService;
import com.qf.electronic.vo.InspectionReceptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wu
 * @since 2023-05-04
 */
@RestController
@RequestMapping("/inspectionReception")
public class InspectionReceptionController {

    @Autowired
    private IInspectionReceptionService inspectionReceptionService;


    @PreAuthorize("hasRole('ROLE_INSPECTION')")
    @PostMapping
    public R<Boolean> addInspectionReception(@Valid @RequestBody InspectionReception reception, BindingResult result){
        if(result.hasFieldErrors()){
            return R.error(ResponseStatus.INVALID_PARAMETER);
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        reception.setOperationUser(user.getUsername());
        return R.ok(inspectionReceptionService.save(reception));
    }

    @PreAuthorize("hasRole('ROLE_INSPECTION')")
    @PutMapping
    public R<Boolean> updateInspectionReception(@Valid @RequestBody InspectionReception reception, BindingResult result){
        if(result.hasFieldErrors()){
            return R.error(ResponseStatus.INVALID_PARAMETER);
        }
        int state = reception.getState();//只有还没有提交去审核的回执才能修改，提交过后的回执是不能修改的
        if(state > 0){
            return R.error(ResponseStatus.ILLEGAL_DATA_STATE);
        }
        UpdateWrapper<InspectionReception> wrapper = new UpdateWrapper<>();
        wrapper.set("defect_type_id", reception.getDefectTypeId());
        wrapper.set("defect_level_id", reception.getDefectLevelId());
        wrapper.set("rate", reception.getRate());
        wrapper.eq("id", reception.getId());
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        wrapper.eq("operation_user", user.getUsername()); //只能是巡检员自己修改自己提交的回执
        return R.ok(inspectionReceptionService.update(wrapper));
    }

    //这个接口是巡检员和线路管理员公用
    //巡检员提交的时候需要改变状态
    //线路管理员审核的时候需要改变状态
    @PutMapping("/state")
    @PreAuthorize("hasAnyRole('ROLE_LINE', 'ROLE_INSPECTION')")
    public R<Boolean> changeInspectionReceptionState(@RequestBody InspectionReceptionStateDto stateDto){
        //当状态修改为2的时候，表示审核通过，审核通过可能会差生消缺任务，同时，巡检任务的状态应该更改为已完成
        return R.ok(inspectionReceptionService.changeInspectionReceptionState(stateDto));
    }


    @PreAuthorize("hasAnyRole('ROLE_LINE', 'ROLE_INSPECTION')")
    @GetMapping
    public R<Page<InspectionReceptionVo>> getInspectionReception(InspectionReceptionDto receptionDto){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        Integer roleId = user.getRoleId();
        if(roleId == 2){
            receptionDto.setRole("ROLE_LINE"); //线路管理员可以查询所有提交的回执
        } else if(roleId == 3){//巡检员只能查询自己的回执
            receptionDto.setRole("ROLE_INSPECTION");
            receptionDto.setOwner(user.getUsername());
        }
        return R.ok(inspectionReceptionService.searchInspectionReceptions(receptionDto));
    }
}